package jinyilw.swing.visuals;

import jinyilw.swing.DragCompUtils;
import jinyilw.swing.visuals.panel.ImagePanel;

import javax.swing.JRootPane;
import javax.swing.JWindow;
import java.awt.Container;
import java.awt.Image;
import java.awt.Window;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

public class ImageWindow extends JWindow // 图像窗
{
	private static final long serialVersionUID = 716505333572120038L;
	protected ImagePanel contentPanel;
	protected boolean rounded = true;

	public ImageWindow()
	{
		initGUI();
	}

	public ImageWindow(Window owner)
	{
		super(owner);
		initGUI();
	}

	public ImageWindow(BufferedImage image)
	{
		super();
		initGUI();
		setBGImage(image);
		setSize(image.getWidth(), image.getHeight());
	}

	private void initGUI()
	{
		contentPanel = new ImagePanel();
		super.setContentPane(contentPanel);
		contentPanel.setLayout(null);
		DragCompUtils.setMovable(contentPanel, this);
	}

	@Override
	protected JRootPane createRootPane()
	{
		JRootPane rp = new NoneRootPane();
		rp.setOpaque(true);
		return rp;
	}

	@Override
	public Container getContentPane()
	{
		return contentPanel;
	}

	public void setBGImage(Image img)
	{
		contentPanel.setImage(img);
	}

	public void setBGImage(Image img, int x, int y, int w, int h)
	{
		contentPanel.setImage(((BufferedImage) img).getSubimage(x, y, w, h));
	}

	public void setBGImageArea(int x, int y, int w, int h)
	{
		contentPanel.setImageArea(x, y, w, h);
	}

	public boolean isRounded()
	{
		return rounded;
	}

	public void setRounded(boolean b)
	{
		rounded = b;
		if (b)
			setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(),
					5, 5));
		else
			setShape(null);
	}

	public void setResizable(boolean b)
	{
		if (b)
			DragCompUtils.setResizeable(contentPanel, this);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		if (width == 0 || height == 0)
			return;
		if (rounded)
			setShape(new RoundRectangle2D.Double(0, 0, width, height, 5, 5));
	}

	public void setOpacity(int opacity)
	{
		contentPanel.setOpacity(opacity);
	}
}
